function getDepartureInfo(i) {
	$.get('/schoolBus/getDepartureInfo', {
		'dcnId' : i
	}, function(data, textStatus) {
		if (!$.isEmptyObject(data)) {
			$('.driverInfo').empty();
			$('.schoolBusInfo').empty();
			jQuery.each(data, function(index, data) {
				var driverInfo = '<label>编号：</label><span>'
						+ data.driver.dNumber
						+ '</span><label>姓名：</label><span>' + data.driver.dName
						+ '</span><label>联系方式：</label><span>'
						+ data.driver.dPhone + '</span><br>';
				$('.driverInfo').append(driverInfo);
				var schoolBusInfo = '<label>编号：</label><span>'
						+ data.schoolBus.sbNumber
						+ '</span><label>载人数：</label><span>'
						+ data.schoolBus.passengerNumber + '</span><br>';
				$('.schoolBusInfo').append(schoolBusInfo);
			});
		} else {
			$('.driverInfo').empty();
			$('.schoolBusInfo').empty();
			$('.driverInfo').html('<span>暂无信息</span>');
			$('.schoolBusInfo').html('<span>暂无信息</span>');
		}
	});
}

function getDynamicCarNumber(str) {
	$.get(str, function(data, textStatus) {
		if (!$.isEmptyObject(data)) {
			getDepartureInfo(data.dcnId);
			$('#carNumberInfo>span').css('display', 'none');
			$('.carNumberInfo').css('display', 'block');
			$('#dcnId').val(data.dcnId);
			$('#dcnDepartureTime').text(data.carNumber.departureTime);
			$('#dcnDeparturePlace').text(data.carNumber.departurePlace);
			$('#dcnDestination').text(data.carNumber.cnDestination);
			$('#departureNumber').text(data.currentPassengerNumber);
			switch (data.dcnStatus) {
			case 0:
				$('#departureStatus').text("预约中、、、");
				$('#departureSchoolBus').attr('disabled', false);
				$('#cancelSchoolBus').attr('disabled', false);
				break;
			case 1:
				$('#departureStatus').text("发车中、、、");
				$('#departureSchoolBus').text('再次发车');
				$('#departureSchoolBus').attr('disabled', false);
				$('#cancelSchoolBus').attr('disabled', true);
				break;
			case 2:
				$('#departureStatus').text("车次已取消！");
				$('#departureSchoolBus').attr('disabled', true);
				$('#cancelSchoolBus').attr('disabled', true);
				break;
			case 3:
				$('#departureStatus').text("车次已完成！");
				$('#driverInfo>span').css('display', 'none');
				$('#schoolBusInfo>span').css('display', 'none');
				$('#departureSchoolBus').attr('disabled', true);
				$('#cancelSchoolBus').attr('disabled', true);
				break;
			default:
				break;
			}
		} else {
			$.messager.show({
				title : '提示',
				msg : '暂无信息！',
				timeout : 3000,
				showType : 'slide'
			});
		}
	});
}

$(function() {
	getDynamicCarNumber('/schoolBus/getCurrentCarNumber');
	if (/^$/.test($('#dcnId').val())) {
		$('#departureSchoolBus').attr('disabled', true);
		$('#cancelSchoolBus').attr('disabled', true);
	}
});
var show = setInterval(getDynamicCarNumber, 10 * 1000,
		'/schoolBus/getCurrentCarNumber');
function reShow() {
	show = setInterval(getDynamicCarNumber, 10 * 1000,
			'/schoolBus/getCurrentCarNumber');
}

$('#leftNumber').click(function() {
	getDynamicCarNumber('/schoolBus/getLeftCarNumber');
});

$('#rightNumber').click(function() {
	getDynamicCarNumber('/schoolBus/getRightCarNumber');
});

function editDynamicCarNumber(i) {
	$.post('/schoolBus/editDynamicCarNumber', {
		'dcnId' : $('#dcnId').val(),
		'status' : i
	}, function(data) {
		reShow();
		getDynamicCarNumber('/schoolBus/getCurrentCarNumber');
		if (data.flag) {
			switch (i) {
			case 1:
				$.messager.show({
					title : '填写发车信息',
					msg : '成功填写',
					timeout : 3000,
					showType : 'slide'
				});
				break;
			case 2:
				$.messager.show({
					title : '取消车次',
					msg : '成功取消车次',
					timeout : 3000,
					showType : 'slide'
				});
				break;
			default:
				break;
			}
		} else {
			$.messager.alert('提示', '发车失败！');
		}
	}, 'json');
}

function editDynamicCarNumberForm() {
	$('#editDynamicCarNumberForm').form('submit', {
		url : '/schoolBus/addDepartureInfo',
		success : function(data) {
			var data = eval('(' + data + ')');
			if (data.flag) {
				editDynamicCarNumber(1);
				$('#editDynamicCarNumber').dialog('close');
			} else {
				reShow();
				$.messager.alert('提示', '发车失败！');
			}
		}
	});
}

$('#departureSchoolBus').click(function() {
	clearInterval(show);
	$('#departureDcnId').val($('#dcnId').val());
	$('#departureDriver').combobox({
		url : '/schoolBus/getDepartureDriver',
		valueField : 'dId',
		textField : 'dNumber',
		required : true,
		width : 155
	});
	$('#departurePassengerNumber').combobox({
		url : '/schoolBus/getDeparturePassengerNumber',
		valueField : 'sbId',
		textField : 'passengerNumber',
		required : true,
		width : 155
	});
	$('#editDynamicCarNumber').dialog({
		title : '填写发车信息',
		modal : true,
		draggable : false,
		buttons : [ {
			text : '确定',
			iconCls : 'icon-save',
			handler : function() {
				editDynamicCarNumberForm();
			}
		}, {
			text : '取消',
			iconCls : 'icon-cancel',
			handler : function() {
				reShow();
				$('#editDynamicCarNumber').dialog('close');
				$.messager.show({
					title : '填写发车信息',
					msg : '取消填写',
					timeout : 3000,
					showType : 'slide'
				});
			}
		} ],
		onClose : function() {
			$('#editDynamicCarNumberForm').form('reset');
		}
	});
});

$('#cancelSchoolBus').click(function() {
	clearInterval(show);
	$.messager.confirm('确认', '确定取消么？', function(r) {
		if (r) {
			editDynamicCarNumber(2);
		} else {
			reShow();
			$.messager.show({
				title : '取消发车',
				msg : '不取消发车',
				timeout : 3000,
				showType : 'slide'
			});
		}
	});
});

$.extend($.fn.validatebox.defaults.rules, {
	equals : {
		validator : function(value, param) {
			return value == $(param[0]).val();
		},
		message : '密码不一致！'
	}
});

$('#secondPasswd').validatebox({
	required : true,
	validType : [ 'equals["#firstPasswd"]' ]
});

$(function() {
	$.get('/schoolBus/getAdminUserInfo', function(data) {
		$('#getAdminUserAccount').text(data.account);
		$('#getAdminUserTime').text(data.time);
	});
});

function editAdminUserPasswdForm() {
	$('#editAdminUserPasswdForm').form('submit', {
		url : '/schoolBus/editAdminUser',
		success : function(data) {
			$('#editAdminUserPasswdDiv').dialog('close');
			var data = eval('(' + data + ')');
			if (data.flag) {
				$.messager.alert('提示', '修改成功,请重新登录！', 'icon-ok', function() {
					logout();
				});
			} else {
				$.messager.alert('提示', '修改失败！');
			}
		}
	});
}

function editAdminUserPasswdDiv() {
	$('#editAdminUserPasswdDiv').dialog({
		title : '修改密码',
		modal : true,
		draggable : false,
		buttons : [ {
			text : '确定',
			iconCls : 'icon-save',
			handler : function() {
				editAdminUserPasswdForm();
			}
		}, {
			text : '取消',
			iconCls : 'icon-cancel',
			handler : function() {
				$('#editAdminUserPasswdDiv').dialog('close');
				$.messager.show({
					title : '修改',
					msg : '取消修改',
					timeout : 3000,
					showType : 'slide'
				});
			}
		} ],
		onClose : function() {
			$('#editAdminUserPasswdForm').form('reset');
		}
	});
}

$('#editAdminUserPasswd').click(function() {
	$('#confirmAdminUserPasswdDiv').dialog({
		title : '确认密码',
		modal : true,
		draggable : false,
		buttons : [ {
			text : '确定',
			iconCls : 'icon-save',
			handler : function() {
				$('#confirmAdminUserPasswdForm').form('submit', {
					url : '/schoolBus/confirmAdminUserPasswd',
					success : function(data) {
						$('#confirmAdminUserPasswdDiv').dialog('close');
						var data = eval('(' + data + ')');
						if (data.flag) {
							$('#adminUserId').val(data.adminUserId);
							editAdminUserPasswdDiv();
						} else {
							$.messager.alert('警告', '验证密码失败！');
						}
					}
				});
			}
		}, {
			text : '取消',
			iconCls : 'icon-cancel',
			handler : function() {
				$('#confirmAdminUserPasswdDiv').dialog('close');
				$.messager.show({
					title : '验证',
					msg : '取消验证',
					timeout : 3000,
					showType : 'slide'
				});
			}
		} ],
		onClose : function() {
			$('#confirmAdminUserPasswdForm').form('reset');
		}
	});
});

function logout() {
	$.post('/schoolBus/adminUserLogout', function(data) {
		if (data.flag) {
			window.location.replace('/schoolBus/mLogin');
		} else {
			$.messager.alert('提示', '网络出错！请刷新页面');
		}
	});
}

$('#adminUserLogout').click(function() {
	$.messager.confirm('确认', '确定退出么？', function(r) {
		if (r) {
			logout();
		} else {
			$.messager.show({
				title : '退出',
				msg : '取消退出',
				timeout : 3000,
				showType : 'slide'
			});
		}
	});

});